package net.huashitong.supervision.task.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.TaskCategory;
import net.huashitong.supervision.task.filter.TaskCategoryFilter;
import net.huashitong.supervision.task.service.TaskCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/8/7
 * Time: 17:02
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Controller
@RequestMapping("/taskCategory")
public class TaskCategoryController extends JsonBaseController {
    @Autowired
    private TaskCategoryService taskCategoryService;
    @Override
    protected String getView(String s) {
        return "/modules/task/category/"+s;
    }
    @RequestMapping(method = RequestMethod.GET)
    public String initPage() {
        return getView("query");
    }

    /**
     * 返回列表
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public JqGrid queryFilter(TaskCategoryFilter filter){
        List<TaskCategory> list = taskCategoryService.query();
        return coverJqGrid(filter,list);
    }
    //返回新增页面
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(){
        return getView("form");
    }

    /**
     * 保存事项类型
     * @param taskCategory
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Message create(@ModelAttribute TaskCategory taskCategory){
        if (StringUtils.isBlank(taskCategory.getType())){
            taskCategory.setType("0");
        }
        taskCategoryService.save(taskCategory);
        return coverMessage("200");
    }
    //返回编辑页面
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(Model model,@PathVariable String id){
        TaskCategory taskCategory = taskCategoryService.getById(id);
        model.addAttribute("taskCategory", taskCategory);
        return getView("edit");
    }
    //保存编辑页面
    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Message edit(TaskCategoryFilter filter){
        TaskCategory taskCategory = taskCategoryService.getById(filter.getSysId());
        if (ObjectUtils.isBlank(taskCategory)){
            return coverMessage("500");
        }
        if (StringUtils.isBlank(filter.getType())){
            filter.setType(taskCategory.getType());
        }
        BeanUtils.copyProperties(filter, taskCategory);
        taskCategoryService.update(taskCategory);
        return coverMessage("200");
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.POST)
    public Message delete(@PathVariable List<String> ids){
        taskCategoryService.batchDelete(ids);
        return coverMessage("200");
    }
}
